const Cart = require("../models/cart");
const Page = require("../models/page");
const ProPic = require("../models/proPic");
const { returnFn } = require("../config/returnFn");

const checkUserLogin = (req, res, next) => {
    console.log("req.session.user=>" + JSON.stringify(req.session.user));
    if (!req.session.user) {
        return returnFn(res, 400, "未登录");
    }
    next();
}

const addCart = (req, res) => {
    let cart = new Cart({
        userId: req.session.user.id,
        productId: req.body.productId ? req.body.productId : '',
        num: req.body.num ? parseInt(req.body.num) : '',
        size: req.body.size ? req.body.size : "",
    });

    Cart.addCart(cart, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常");
        returnFn(res, 200, "success", result);
    })
}
const updateCart = (req, res) => {
    let cart = new Cart({
        id: parseInt(req.body.id),
        num: req.body.num ? parseInt(req.body.num) : '',
        size: req.body.size ? req.body.size : ''
    });
    Cart.updateCart(cart, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常");
        returnFn(res, 200, "success", result);
    })
}
const deleteCart = (req, res) => {
    Cart.deleteCart(req.query.id, (err, result) => {
        if (err) return returnFn(res, 503, "数据库异常");

        returnFn(res, 200, "success", result);
    })
}
const queryCart = (req, res) => {
    Cart.queryCart(req.session.user.id, (err, data) => {
        if (err) return returnFn(res, 503, "数据库异常");

        // console.log(`data=>${JSON.stringify(data)}`);
        if (data.length == 0) return res.send(data);

        let idStr = "";
        for (let i = 0; i < data.length; i++) {
            if (i == 0) idStr += data[i].productId;
            else idStr += "," + data[i].productId;
            data[i].pic = new Array();
        }

        ProPic.queryPic(idStr, (err, picData) => {
            if (err) return returnFn(res, 503, "数据库异常");

            for (let l = 0; l < picData.length; l++) {
                for (let n = 0; n < data.length; n++) {
                    if (data[n].productId == picData[l].productId) {
                        data[n].pic[data[n].pic.length] = picData[l];
                    }
                }
            }
            returnFn(res, 200, "success", data);
        })
    })
}
const queryCartPaging = (req, res) => {
    let page = new Page({
        page: req.query.page ? parseInt(req.query.page) : 1,
        size: req.query.size ? parseInt(req.query.size) : 10
    });
    Cart.queryCartPaging(req.session.user.id, page, (err, data) => {
        console.log(`err1=>${JSON.stringify(err)}`);
        console.log(`data=>${JSON.stringify(data)}`);
        if (err) return returnFn(res, 503, "数据库异常");

        Cart.countCart(req.session.user.id, (err, result) => {
            console.log(`err2=>${JSON.stringify(err)}`);
            console.log(`result=>${JSON.stringify(result)}`);
            if (err) return returnFn(res, 503, "数据库异常");

            if (data.length == 0) {
                page.count = result.count;
                page.data = data;
                return returnFn(res, 200, "success", data);
            }

            let idStr = "";
            for (let i = 0; i < data.length; i++) {
                if (i == 0) idStr += data[i].productId;
                else idStr += "," + data[i].productId;
                data[i].pic = new Array();
            }

            ProPic.queryPic(idStr, (err, picData) => {
                console.log(`err3=>${JSON.stringify(err)}`);
                console.log(`picData=>${JSON.stringify(picData)}`);
                if (err) return returnFn(res, 503, "数据库异常");

                for (let l = 0; l < picData.length; l++) {
                    for (let n = 0; n < data.length; n++) {
                        if (data[n].productId == picData[l].productId) {
                            data[n].pic[data[n].pic.length] = picData[l];
                        }
                    }
                }
                page.count = result.count;
                page.data = data;
                returnFn(res, 200, "success", page);
            });
        });
    });
}

module.exports = {
    checkUserLogin,
    addCart,
    updateCart,
    deleteCart,
    queryCart,
    queryCartPaging,
}